package logic

import (
	"context"
	"errors"
	"work-safety/model/model"

	"work-safety/workrpc/internal/svc"
	"work-safety/workrpc/workrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type LoginLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LoginLogic {
	return &LoginLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *LoginLogic) Login(in *workrpc.LoginRequest) (*workrpc.LoginResponse, error) {
	// todo: add your logic here and delete this line
	var shop model.Shop
	model.DB.Debug().Where("phone=?", in.Phone).First(&shop)

	if shop.Password != in.Phone {
		return nil, errors.New("密码输入错误")
	}

	shop = model.Shop{
		Phone:    in.Phone,
		Password: in.Password,
		Name:     in.Name,
	}
	model.DB.Debug().Find(&shop)
	return &workrpc.LoginResponse{
		ShopId: int64(shop.ID),
	}, nil
}
